package cn.caishen.report.common.aop;

import cn.caishen.report.common.constant.DatasourceConstant;
import cn.caishen.report.common.dynamic.DynamicDataSourceService;
import com.alibaba.druid.pool.DruidDataSource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DatasourceAop {

    @Around(value = "execution( * cn.caishen.report.controller..*(..))")
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(DatasourceConstant.url);
        dataSource.setPassword(DatasourceConstant.password);
        dataSource.setUsername(DatasourceConstant.userName);
        dataSource.setDriverClassName(DatasourceConstant.driverClassName);
        DynamicDataSourceService.addDataSource(DatasourceConstant.DATASOURCE_DEFAULT_KEY, dataSource);
        DynamicDataSourceService.switchDb(DatasourceConstant.DATASOURCE_DEFAULT_KEY);
        return pjp.proceed();
    }
}


